package server.datamodel;

import java.util.HashMap;

public class Map {

	private HashMap<String, Waypoint> waypoints= new HashMap<String, Waypoint>();
	private HashMap<String, Cell> cells= new HashMap<String, Cell>();
	private String ID;
	private String background;
	private String name;
	private Tile tile;
	private String createdBy; 
	private int version;
	private Waypoint enterWaypoint;
	private Waypoint exitWaypoint;
	
	public Map() {
		this.ID = Datamodel.generateID(this);
	}
	
	@Override
	public String toString() {
		return ID;
	}
	
	public void addWaypoint(Waypoint waypoint) {
		if(!waypoints.containsKey(waypoint.getID())) {
			waypoints.put(waypoint.getID(), waypoint);
			waypoint.setMap(this);
		}
	}
	
	public void removeWaypoint(Waypoint waypoint) {
		if(waypoints.remove(waypoint.getID()) != null) {
			waypoint.setMap(null);
		}
	}
	
	public HashMap<String, Waypoint> getWaypoints() {
		return waypoints;
	}
	
	public void addCell(Cell cell) {
		if(!cells.containsKey(cell.getID())) {
			cells.put(cell.getID(), cell);
			cell.setMap(this);
		}
	}
	
	public void removeCell(Cell cell) {
		if(cells.remove(cell.getID()) != null) {
			cell.setMap(null);
		}
	}

	public String getID() {
		return ID;
	}

	public void setID(String ID) {
		this.ID = ID;
	}

	public String getBackground() {
		return background;
	}

	public void setBackground(String background) {
		this.background = background;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCreatedBy() {
		return createdBy;
	}

	public void setCreatedBy(String createdBy) {
		this.createdBy = createdBy;
	}

	public Tile getTile() {
		return tile;
	}

	public Waypoint getEnterWaypoint() {
		return enterWaypoint;
	}

	public Waypoint getExitWaypoint() {
		return exitWaypoint;
	}
	
	public Waypoint getWaypoint(String ID) {
		return waypoints.get(ID);
	}
	
	public Cell getCell(String ID) {
		return cells.get(ID);
	}
	
	public HashMap<String, Cell> getCells() {
		return cells;
	}
	
	public void setTile(Tile tile) {
		if(this.tile != tile) {
			if(getTile() != null) {
				Tile oldTile = getTile();
				this.tile = null;
				oldTile.setMap(null);
			}
			this.tile = tile;
			if(getTile() != null) {
				getTile().setMap(this);
			}
		}
	}
	
	public void setEnterWaypoint(Waypoint enterWaypoint) {
		if(this.enterWaypoint != enterWaypoint) {
			if(getEnterWaypoint() != null) {
				Waypoint oldWaypoint = getEnterWaypoint();
				this.enterWaypoint = null;
				oldWaypoint.setMap(null);
			}
			this.enterWaypoint = enterWaypoint;
			if(getEnterWaypoint() != null) {
				getEnterWaypoint().setMap(this);
			}
		}
	}
	
	public void setExitWaypoint(Waypoint exitWaypoint) {
		if(this.exitWaypoint != exitWaypoint) {
			if(getExitWaypoint() != null) {
				Waypoint oldWaypoint = getExitWaypoint();
				this.exitWaypoint = null;
				oldWaypoint.setMap(null);
			}
			this.exitWaypoint = exitWaypoint;
			if(getExitWaypoint() != null) {
				getExitWaypoint().setMap(this);
			}
		}
	}

	public int getVersion() {
		return version;
	}

	public void setVersion(int version) {
		this.version = version;
	}
	
	public Waypoint getWaypointByIndex(int index) {
		Waypoint w = null;
		for (Waypoint waypoint : waypoints.values()) {
			if (waypoint.getIndex() == index) {
				w = waypoint;
				break;
			}
		}
		return w;
	}
	
	
	
	public Cell getByXandY (int x, int y) {
		Cell cell1 =  null;
		for (Cell cell : cells.values()) {
			if (cell.getX() == x && cell.getY() == y) {
				cell1 = cell;
				break;
			}
		}
		return cell1;
	}
	
}
